extends CenterContainer


onready var z_op = $PanelContainer/VBoxContainer/z/OptionButton
onready var desc_input = $PanelContainer/VBoxContainer/desc/LineEdit
onready var type_op = $PanelContainer/VBoxContainer/type/OptionButton
onready var dialog_op = $PanelContainer/VBoxContainer/dialog/DialogSelect
onready var res_input = $PanelContainer/VBoxContainer/res/LineEdit
onready var res_frame_input = $PanelContainer/VBoxContainer/resFrame/SpinBox
onready var audio_input = $PanelContainer/VBoxContainer/audio/LineEdit

var edit_data = null




func set_edit_data(data):
	edit_data = data
	
	if edit_data:
		$PanelContainer/VBoxContainer/position/position.text = str("(",data.x, " ", data.y, ")")
		z_op.select(z_op.get_item_index(data["z"]))
		desc_input.text = edit_data["description"]
		
		type_op.select(type_op.get_item_index(data["type"]))
		res_input.text = edit_data["res"]
		dialog_op.select_value(edit_data["dialogId"])
		res_frame_input.value = edit_data["resFrame"]
		audio_input.text = edit_data["audio"]
		
		$PanelContainer/VBoxContainer/modalTitle.text = "编辑障碍物"
		
	else:
		pass

func state_update():
	var map_view = MainTool.getUILayer().get_ui("MapView")
	
	# 更新当前坐标
	var position_info = map_view.get_select_position_info()
	$PanelContainer/VBoxContainer/position/position.text = str("(",position_info.x, " ", position_info.y, ")")
	

func _on_ok_button_up():
	var map_view = MainTool.getUILayer().get_ui("MapView")
	
	var position_info = map_view.get_select_position_info()
	
	
	
	if edit_data:
		read_form(edit_data)
		
		DataSource.update_data(DataSource.map_interactive, edit_data)
		
		MainTool.toast_info("更新障碍物成功!")
	else:
		var data = {
			"x": position_info.x,
			"y": position_info.y,
			"mapId": position_info["map_register"]["id"]
		}
		
		read_form(data)
	
		var pre_list = DataSource.get_data_by_list(DataSource.map_interactive, position_info["map_register"]["id"], "mapId")
		for pre in pre_list:
			if pre["x"] == position_info.x and pre["y"] == position_info.y:
				MainTool.toast_info("该位置不可重复添加!")
				return
	
		DataSource.add_data(DataSource.map_interactive, data)
		
		MainTool.toast_info("添加障碍物成功!")
	
	map_view.entity_update()
	
	MainTool.getUILayer().pop_ui()


func read_form(dic):
	dic["z"] = z_op.get_selected_id()
	dic["description"] = desc_input.text
	dic["type"] = type_op.get_selected_id()
	dic["res"] = res_input.text
	dic["dialogId"] = dialog_op.get_value()
	dic["resFrame"] = res_frame_input.value
	dic["audio"] = audio_input.text

func _on_cancel_button_up():
	MainTool.getUILayer().pop_ui()
